using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.Caching;
using System.Xml.Serialization;
using Medianamik.Core;
using Medianamik.Core.Configuration;
using Medianamik.Core.DAL;
using Medianamik.Core.Sugar;
using SubSonic;
using ConfigurationSectionName = Medianamik.Core.ConfigurationSectionName;
/* Ajouter les Using nécessaires à la compilation */

namespace Medianamik.Client.Data
{
    /// <summary>
    ///   Strongly-typed collection for the MedianamikRedirect class.
    /// </summary>
    [Serializable]
    public class MedianamikRedirectCollection : ReadOnlyList<MedianamikRedirect, MedianamikRedirectCollection>,
                                                ICollectionWithChildren
                                                    <MedianamikRedirectCollection, MedianamikRedirect>
    {
        public new IEnumerator<MedianamikRedirect> GetEnumerator()
        {
            return base.GetEnumerator();
        }

        public MedianamikRedirectCollection Load(int pPageSize, int pPageIndex)
        {
            if (orderByCollection.Count == 0)
            {
                AddDefaultOrderBy();
            }

            var qry = new Query(new MedianamikRedirect().GetSchema());
            CheckLogicalDelete(qry);
            foreach (var where in wheres)
                qry.AddWhere(where);
            foreach (var between in betweens)
                qry.AddBetweenAnd(between);
            qry.OrderByCollection = orderByCollection;
            qry.PageSize = pPageSize;
            qry.PageIndex = pPageIndex;
            using (var rdr = qry.ExecuteReader())
            {
                LoadAndCloseReader(rdr);
                _totalCount = qry.GetRecordCount();
                return this;
            }
        }

        public MedianamikRedirectCollection Load(int pMaxElements)
        {
            return Load(pMaxElements, 1);
        }

        public MedianamikRedirectCollection Load(ContentState contentState)
        {
            wheres.RemoveAll(w => w.ColumnName.Equals("Generation"));
            Where("Generation", (int) contentState);
            return Load();
        }

        public MedianamikRedirectCollection Load(string cultureName, ContentState contentState, ActiveStatus status)
        {
            wheres.RemoveAll(w => w.ColumnName.Equals("CultureName"));
            Where("CultureName", cultureName);
            wheres.RemoveAll(w => w.ColumnName.Equals("Generation"));
            Where("Generation", (int) contentState);
            wheres.RemoveAll(w => w.ColumnName.Equals("IsActive"));
            if (status != ActiveStatus.All)
            {
                Where("IsActive", status == ActiveStatus.Activated ? true : false);
            }

            return Load();
        }

        public IEnumerable<MedianamikRedirect> LoadByIDs(string ids)
        {
            return LoadByIDs(DataUtil.GetSelectedGuids(ids));
        }

        public IEnumerable<MedianamikRedirect> LoadByIDs(IList<Guid> ids)
        {
            if (ids.Count > 0)
            {
                if (ids.Count == 1)
                    Where("NodeId", ids.First());
                else
                    Where("NodeId", Comparison.In, ids.Select(g => g.ToString()));

                return Load().ReorderByIds(ids);
            }

            return Enumerable.Empty<MedianamikRedirect>();
        }

        public IEnumerable<MedianamikRedirect> LoadByIDs(string ids, string cultureName)
        {
            return LoadByIDs(DataUtil.GetSelectedGuids(ids), cultureName);
        }

        public IEnumerable<MedianamikRedirect> LoadByIDs(IList<Guid> ids, string cultureName)
        {
            wheres.RemoveAll(w => w.ColumnName.Equals("CultureName"));
            Where("CultureName", cultureName);

            return LoadByIDs(ids);
        }

        public IEnumerable<MedianamikRedirect> LoadByIDs(string ids, string cultureName, ContentState contentState,
                                                         ActiveStatus status)
        {
            return LoadByIDs(DataUtil.GetSelectedGuids(ids), cultureName, contentState, status);
        }

        public IEnumerable<MedianamikRedirect> LoadByIDs(IList<Guid> ids, string cultureName, ContentState contentState,
                                                         ActiveStatus status)
        {
            if (ids.Count > 0)
            {
                if (ids.Count == 1)
                    Where("NodeId", ids.First());
                else
                    Where("NodeId", Comparison.In, ids.Select(g => g.ToString()));
                return Load(cultureName, contentState, status)
                    .ReorderByIds(ids);
            }
            return Enumerable.Empty<MedianamikRedirect>();
        }

        public new MedianamikRedirectCollection Load()
        {
            if (orderByCollection.Count == 0)
            {
                AddDefaultOrderBy();
            }
            return base.Load();
        }

        private void AddDefaultOrderBy()
        {
            OrderByAsc("Position");
            OrderByDesc("CreatedOn");
        }


        public MedianamikRedirectCollection(ContentState state)
            : this(CultureInfo.CurrentUICulture.Name, default(Guid?), (int) state, ActiveStatus.Activated)
        {
        }

        public MedianamikRedirectCollection()
            : this(CultureInfo.CurrentUICulture.Name, default(Guid?), default(int?), ActiveStatus.Activated)
        {
        }

        public MedianamikRedirectCollection(Guid pTypeId)
            : this(CultureInfo.CurrentUICulture.Name, pTypeId, default(int?), ActiveStatus.Activated)
        {
        }


        public MedianamikRedirectCollection(string pCultureName)
            : this(pCultureName, default(Guid?), default(int?), ActiveStatus.Activated)
        {
        }

        public MedianamikRedirectCollection(string pCultureName, Guid pTypeId)
            : this(pCultureName, pTypeId, default(int?), ActiveStatus.Activated)
        {
        }

        public MedianamikRedirectCollection(string pCultureName, ContentState state)
            : this(pCultureName, default(Guid?), (int) state, ActiveStatus.Activated)
        {
        }

        public MedianamikRedirectCollection(string pCultureName, Guid pTypeId, ContentState state)
            : this(pCultureName, pTypeId, (int) state, ActiveStatus.Activated)
        {
        }

        private MedianamikRedirectCollection(string pCultureName, Guid? pTypeId, int? pContentState, ActiveStatus status)
        {
            if (pContentState.HasValue)
            {
                CurrentGeneration = pContentState.Value;
            }
            else
            {
                CurrentGeneration = HttpContext.Current == null
                                        ? (int) ContentState.Published
                                        : new Utility().GetGeneration(HttpContext.Current.Request);
            }

            if (status != ActiveStatus.All)
            {
                Where("IsActive", status == ActiveStatus.Activated ? true : false);
            }

            Where("Generation", CurrentGeneration);
            Where("CultureName", pCultureName);
            if (pTypeId.HasValue)
            {
                Where("TypeId", pTypeId.Value);
            }
        }

        public MedianamikRedirectCollection LoadParentNodes(Guid pNodeId)
        {
            return LoadParentNodes(pNodeId, false, 0, (ContentState) CurrentGeneration, ActiveStatus.Activated);
        }

        public MedianamikRedirectCollection LoadParentNodes(Guid pNodeId, bool pInclusive)
        {
            return LoadParentNodes(pNodeId, pInclusive, 0, (ContentState) CurrentGeneration, ActiveStatus.Activated);
        }

        public MedianamikRedirectCollection LoadParentNodes(Guid pNodeId, bool pInclusive, int pMaxDepth)
        {
            return LoadParentNodes(pNodeId, pInclusive, pMaxDepth, (ContentState) CurrentGeneration,
                                   ActiveStatus.Activated);
        }

        public MedianamikRedirectCollection LoadParentNodes(Guid pNodeId, bool pInclusive, int pMaxDepth,
                                                            ContentState contentState)
        {
            return LoadParentNodes(pNodeId, pInclusive, pMaxDepth, contentState, ActiveStatus.Activated);
        }

        public MedianamikRedirectCollection LoadParentNodes(Guid pNodeId, bool pInclusive, int pMaxDepth,
                                                            ContentState contentState, ActiveStatus status)
        {
            return LoadParentNodes(pNodeId,
                                   wheres.First(w => w.ColumnName.Equals("CultureName")).ParameterValue.ToString(),
                                   pInclusive, pMaxDepth, contentState, status);
        }

        public MedianamikRedirectCollection LoadParentNodes(Guid pNodeId, string cultureName, bool pInclusive,
                                                            int pMaxDepth,
                                                            ContentState contentState, ActiveStatus status)
        {
            var isActiveValue = -1;
            switch (status)
            {
                case ActiveStatus.Activated:
                    isActiveValue = 1;
                    break;
                case ActiveStatus.Deactivated:
                    isActiveValue = 0;
                    break;
            }
            var sp = new StoredProcedure("medianamik_LoadParentNodes");
            sp.Command.AddParameter("@TableName", "vw_gen_MedianamikRedirect", DbType.String);
            sp.Command.AddParameter("@NodeId", pNodeId, DbType.Guid);
            sp.Command.AddParameter("@CultureName", cultureName, DbType.String);
            sp.Command.AddParameter("@Generation", (int) contentState, DbType.Int32);
            sp.Command.AddParameter("@MaxDepth", pMaxDepth, DbType.Int32);
            sp.Command.AddParameter("@IsActive", isActiveValue, DbType.Int32);
            sp.Command.AddParameter("@InclusiveChar", DataUtil.GetSQLInclusivityOperator(pInclusive), DbType.String);
            var rdr = sp.GetReader();
            Load(rdr);
            rdr.Close();
            return this;
        }

        public MedianamikRedirectCollection LoadChildNodes(Guid pNodeId)
        {
            return LoadChildNodes(pNodeId, false, 0, (ContentState) CurrentGeneration, ActiveStatus.Activated);
        }

        public MedianamikRedirectCollection LoadChildNodes(Guid pNodeId, bool pInclusive)
        {
            return LoadChildNodes(pNodeId, pInclusive, 0, (ContentState) CurrentGeneration, ActiveStatus.Activated);
        }

        public MedianamikRedirectCollection LoadChildNodes(Guid pNodeId, bool pInclusive, int pMaxDepth)
        {
            return LoadChildNodes(pNodeId, pInclusive, pMaxDepth, (ContentState) CurrentGeneration,
                                  ActiveStatus.Activated);
        }

        public MedianamikRedirectCollection LoadChildNodes(Guid pNodeId, bool pInclusive, int pMaxDepth,
                                                           ContentState contentState)
        {
            return LoadChildNodes(pNodeId, pInclusive, pMaxDepth, contentState, ActiveStatus.Activated);
        }

        public MedianamikRedirectCollection LoadChildNodes(Guid pNodeId, bool pInclusive, int pMaxDepth,
                                                           ContentState contentState, ActiveStatus status)
        {
            return LoadChildNodes(pNodeId,
                                  wheres.First(w => w.ColumnName.Equals("CultureName")).ParameterValue.ToString(),
                                  pInclusive, pMaxDepth, contentState, status);
        }

        public MedianamikRedirectCollection LoadChildNodes(Guid pNodeId, string cultureName, bool pInclusive,
                                                           int pMaxDepth,
                                                           ContentState contentState, ActiveStatus status)
        {
            var isActiveValue = -1;
            switch (status)
            {
                case ActiveStatus.Activated:
                    isActiveValue = 1;
                    break;
                case ActiveStatus.Deactivated:
                    isActiveValue = 0;
                    break;
            }
            var sp = new StoredProcedure("medianamik_LoadChildNodes");
            sp.Command.AddParameter("@TableName", "vw_gen_MedianamikRedirect", DbType.String);
            sp.Command.AddParameter("@NodeId", pNodeId, DbType.Guid);
            sp.Command.AddParameter("@CultureName", cultureName, DbType.String);
            sp.Command.AddParameter("@Generation", contentState, DbType.Int32);
            sp.Command.AddParameter("@MaxDepth", pMaxDepth, DbType.Int32);
            sp.Command.AddParameter("@IsActive", isActiveValue, DbType.Int32);
            sp.Command.AddParameter("@InclusiveChar", DataUtil.GetSQLInclusivityOperator(pInclusive), DbType.String);
            var rdr = sp.GetReader();
            Load(rdr);
            rdr.Close();
            return this;
        }

        public const string VIEW_NAME = "vw_gen_MedianamikRedirect";
        public static readonly Guid TYPE_ID = new Guid("30224fd8-c92d-4498-b159-e916292b336c");

        public int CurrentGeneration { get; private set; }

        private int _totalCount;

        /// <summary>
        ///   Full items count (when paging is used)
        /// </summary>
        public int TotalCount
        {
            get { return _totalCount == 0 ? Count : _totalCount; }
        }
    }

    /// <summary>
    ///   This is  Read-only wrapper class for the vw_gen_MedianamikRedirect view.
    /// </summary>
    [Serializable]
    public class MedianamikRedirect : ReadOnlyRecord<MedianamikRedirect>, IReadOnlyRecord, IRecordBase,
                                      IMedianamikRedirect
    {
        public const string VIEW_NAME = "vw_gen_MedianamikRedirect";
        public static readonly Guid TYPE_ID = new Guid("30224fd8-c92d-4498-b159-e916292b336c");
        public static readonly string TYPE_NAME = "MedianamikRedirect";

        private static readonly MedianamikConfigSection CONFIG =
            (MedianamikConfigSection) ConfigurationManager.GetSection(ConfigurationSectionName.MEDIANAMIK);

        public int CurrentGeneration { get; private set; }

        protected static void SetSQLProps()
        {
            GetTableSchema();
        }

        public static TableSchema.Table Schema
        {
            get
            {
                if (BaseSchema == null)
                {
                    SetSQLProps();
                }
                return BaseSchema;
            }
        }

        private static void GetTableSchema()
        {
            if (!IsSchemaInitialized)
            {
                //Schema declaration
                var schema = new TableSchema.Table("vw_gen_MedianamikRedirect", TableType.View,
                                                   DataService.GetInstance("CodeGenerationProvider"));
                schema.Columns = new TableSchema.TableColumnCollection();
                schema.SchemaName = "dbo";
                //columns

                var colvarId = new TableSchema.TableColumn(schema);
                colvarId.ColumnName = "Id";
                colvarId.DataType = DbType.Guid;
                colvarId.MaxLength = 0;
                colvarId.AutoIncrement = false;
                colvarId.IsNullable = false;
                colvarId.IsPrimaryKey = false;
                colvarId.IsForeignKey = false;
                colvarId.IsReadOnly = false;

                schema.Columns.Add(colvarId);

                var colvarNodeId = new TableSchema.TableColumn(schema);
                colvarNodeId.ColumnName = "NodeId";
                colvarNodeId.DataType = DbType.Guid;
                colvarNodeId.MaxLength = 0;
                colvarNodeId.AutoIncrement = false;
                colvarNodeId.IsNullable = false;
                colvarNodeId.IsPrimaryKey = false;
                colvarNodeId.IsForeignKey = false;
                colvarNodeId.IsReadOnly = false;

                schema.Columns.Add(colvarNodeId);

                var colvarRevision = new TableSchema.TableColumn(schema);
                colvarRevision.ColumnName = "Revision";
                colvarRevision.DataType = DbType.Int32;
                colvarRevision.MaxLength = 0;
                colvarRevision.AutoIncrement = false;
                colvarRevision.IsNullable = true;
                colvarRevision.IsPrimaryKey = false;
                colvarRevision.IsForeignKey = false;
                colvarRevision.IsReadOnly = false;

                schema.Columns.Add(colvarRevision);

                var colvarCultureName = new TableSchema.TableColumn(schema);
                colvarCultureName.ColumnName = "CultureName";
                colvarCultureName.DataType = DbType.String;
                colvarCultureName.MaxLength = 10;
                colvarCultureName.AutoIncrement = false;
                colvarCultureName.IsNullable = false;
                colvarCultureName.IsPrimaryKey = false;
                colvarCultureName.IsForeignKey = false;
                colvarCultureName.IsReadOnly = false;

                schema.Columns.Add(colvarCultureName);

                var colvarIsActive = new TableSchema.TableColumn(schema);
                colvarIsActive.ColumnName = "IsActive";
                colvarIsActive.DataType = DbType.Boolean;
                colvarIsActive.MaxLength = 0;
                colvarIsActive.AutoIncrement = false;
                colvarIsActive.IsNullable = false;
                colvarIsActive.IsPrimaryKey = false;
                colvarIsActive.IsForeignKey = false;
                colvarIsActive.IsReadOnly = false;

                schema.Columns.Add(colvarIsActive);

                var colvarIsSubEntity = new TableSchema.TableColumn(schema);
                colvarIsSubEntity.ColumnName = "IsSubEntity";
                colvarIsSubEntity.DataType = DbType.Boolean;
                colvarIsSubEntity.MaxLength = 0;
                colvarIsSubEntity.AutoIncrement = false;
                colvarIsSubEntity.IsNullable = false;
                colvarIsSubEntity.IsPrimaryKey = false;
                colvarIsSubEntity.IsForeignKey = false;
                colvarIsSubEntity.IsReadOnly = false;

                schema.Columns.Add(colvarIsSubEntity);

                var colvarGeneration = new TableSchema.TableColumn(schema);
                colvarGeneration.ColumnName = "Generation";
                colvarGeneration.DataType = DbType.Int32;
                colvarGeneration.MaxLength = 0;
                colvarGeneration.AutoIncrement = false;
                colvarGeneration.IsNullable = false;
                colvarGeneration.IsPrimaryKey = false;
                colvarGeneration.IsForeignKey = false;
                colvarGeneration.IsReadOnly = false;

                schema.Columns.Add(colvarGeneration);

                var colvarHasModifications = new TableSchema.TableColumn(schema);
                colvarHasModifications.ColumnName = "HasModifications";
                colvarHasModifications.DataType = DbType.Boolean;
                colvarHasModifications.MaxLength = 0;
                colvarHasModifications.AutoIncrement = false;
                colvarHasModifications.IsNullable = false;
                colvarHasModifications.IsPrimaryKey = false;
                colvarHasModifications.IsForeignKey = false;
                colvarHasModifications.IsReadOnly = false;

                schema.Columns.Add(colvarHasModifications);

                var colvarCreatedOn = new TableSchema.TableColumn(schema);
                colvarCreatedOn.ColumnName = "CreatedOn";
                colvarCreatedOn.DataType = DbType.DateTime;
                colvarCreatedOn.MaxLength = 0;
                colvarCreatedOn.AutoIncrement = false;
                colvarCreatedOn.IsNullable = false;
                colvarCreatedOn.IsPrimaryKey = false;
                colvarCreatedOn.IsForeignKey = false;
                colvarCreatedOn.IsReadOnly = false;

                schema.Columns.Add(colvarCreatedOn);

                var colvarDepth = new TableSchema.TableColumn(schema);
                colvarDepth.ColumnName = "Depth";
                colvarDepth.DataType = DbType.Int32;
                colvarDepth.MaxLength = 0;
                colvarDepth.AutoIncrement = false;
                colvarDepth.IsNullable = false;
                colvarDepth.IsPrimaryKey = false;
                colvarDepth.IsForeignKey = false;
                colvarDepth.IsReadOnly = false;

                schema.Columns.Add(colvarDepth);

                var colvarParentNodeId = new TableSchema.TableColumn(schema);
                colvarParentNodeId.ColumnName = "ParentNodeId";
                colvarParentNodeId.DataType = DbType.Guid;
                colvarParentNodeId.MaxLength = 0;
                colvarParentNodeId.AutoIncrement = false;
                colvarParentNodeId.IsNullable = true;
                colvarParentNodeId.IsPrimaryKey = false;
                colvarParentNodeId.IsForeignKey = false;
                colvarParentNodeId.IsReadOnly = false;

                schema.Columns.Add(colvarParentNodeId);

                var colvarPath = new TableSchema.TableColumn(schema);
                colvarPath.ColumnName = "Path";
                colvarPath.DataType = DbType.String;
                colvarPath.MaxLength = 500;
                colvarPath.AutoIncrement = false;
                colvarPath.IsNullable = false;
                colvarPath.IsPrimaryKey = false;
                colvarPath.IsForeignKey = false;
                colvarPath.IsReadOnly = false;

                schema.Columns.Add(colvarPath);

                var colvarPosition = new TableSchema.TableColumn(schema);
                colvarPosition.ColumnName = "Position";
                colvarPosition.DataType = DbType.Int16;
                colvarPosition.MaxLength = 0;
                colvarPosition.AutoIncrement = false;
                colvarPosition.IsNullable = false;
                colvarPosition.IsPrimaryKey = false;
                colvarPosition.IsForeignKey = false;
                colvarPosition.IsReadOnly = false;

                schema.Columns.Add(colvarPosition);

                var colvarTypeId = new TableSchema.TableColumn(schema);
                colvarTypeId.ColumnName = "TypeId";
                colvarTypeId.DataType = DbType.Guid;
                colvarTypeId.MaxLength = 0;
                colvarTypeId.AutoIncrement = false;
                colvarTypeId.IsNullable = false;
                colvarTypeId.IsPrimaryKey = false;
                colvarTypeId.IsForeignKey = false;
                colvarTypeId.IsReadOnly = false;

                schema.Columns.Add(colvarTypeId);

                var colvarModifiedOn = new TableSchema.TableColumn(schema);
                colvarModifiedOn.ColumnName = "ModifiedOn";
                colvarModifiedOn.DataType = DbType.DateTime;
                colvarModifiedOn.MaxLength = 0;
                colvarModifiedOn.AutoIncrement = false;
                colvarModifiedOn.IsNullable = true;
                colvarModifiedOn.IsPrimaryKey = false;
                colvarModifiedOn.IsForeignKey = false;
                colvarModifiedOn.IsReadOnly = false;

                schema.Columns.Add(colvarModifiedOn);

                var colvarCreatedBy = new TableSchema.TableColumn(schema);
                colvarCreatedBy.ColumnName = "CreatedBy";
                colvarCreatedBy.DataType = DbType.String;
                colvarCreatedBy.MaxLength = 100;
                colvarCreatedBy.AutoIncrement = false;
                colvarCreatedBy.IsNullable = true;
                colvarCreatedBy.IsPrimaryKey = false;
                colvarCreatedBy.IsForeignKey = false;
                colvarCreatedBy.IsReadOnly = false;

                schema.Columns.Add(colvarCreatedBy);

                var colvarQueryString = new TableSchema.TableColumn(schema);
                colvarQueryString.ColumnName = "QueryString";
                colvarQueryString.DataType = DbType.AnsiString;
                colvarQueryString.MaxLength = 256;
                colvarQueryString.AutoIncrement = false;
                colvarQueryString.IsNullable = true;
                colvarQueryString.IsPrimaryKey = false;
                colvarQueryString.IsForeignKey = false;
                colvarQueryString.IsReadOnly = false;

                schema.Columns.Add(colvarQueryString);

                var colvarRedirectFrom = new TableSchema.TableColumn(schema);
                colvarRedirectFrom.ColumnName = "RedirectFrom";
                colvarRedirectFrom.DataType = DbType.AnsiString;
                colvarRedirectFrom.MaxLength = 500;
                colvarRedirectFrom.AutoIncrement = false;
                colvarRedirectFrom.IsNullable = true;
                colvarRedirectFrom.IsPrimaryKey = false;
                colvarRedirectFrom.IsForeignKey = false;
                colvarRedirectFrom.IsReadOnly = false;

                schema.Columns.Add(colvarRedirectFrom);

                var colvarRedirectTo = new TableSchema.TableColumn(schema);
                colvarRedirectTo.ColumnName = "RedirectTo";
                colvarRedirectTo.DataType = DbType.Guid;
                colvarRedirectTo.MaxLength = 0;
                colvarRedirectTo.AutoIncrement = false;
                colvarRedirectTo.IsNullable = true;
                colvarRedirectTo.IsPrimaryKey = false;
                colvarRedirectTo.IsForeignKey = false;
                colvarRedirectTo.IsReadOnly = false;

                schema.Columns.Add(colvarRedirectTo);

                var colvarRedirectCode = new TableSchema.TableColumn(schema);
                colvarRedirectCode.ColumnName = "RedirectCode";
                colvarRedirectCode.DataType = DbType.Int32;
                colvarRedirectCode.MaxLength = 0;
                colvarRedirectCode.AutoIncrement = false;
                colvarRedirectCode.IsNullable = true;
                colvarRedirectCode.IsPrimaryKey = false;
                colvarRedirectCode.IsForeignKey = false;
                colvarRedirectCode.IsReadOnly = false;

                schema.Columns.Add(colvarRedirectCode);


                BaseSchema = schema;
                //add this schema to the provider
                //so we can query it later
                DataService.Providers["CodeGenerationProvider"].AddSchema("vw_gen_MedianamikRedirect", schema);
            }
        }

        public static Query CreateQuery()
        {
            return new Query(Schema);
        }

        public MedianamikRedirect()
        {
            SetSQLProps();
            SetDefaults();
            MarkNew();
        }

        private MedianamikRedirect(bool useDatabaseDefaults)
        {
            SetSQLProps();
            if (useDatabaseDefaults)
            {
                ForceDefaults();
            }
            MarkNew();
        }

        private MedianamikRedirect(object keyID)
        {
            SetSQLProps();
            LoadByKey(keyID);
        }

        private MedianamikRedirect(string columnName, object columnValue)
        {
            SetSQLProps();
            LoadByParam(columnName, columnValue);
        }

        public MedianamikRedirect(Guid pNodeId, string pCultureName, ContentState state)
            : this(pNodeId, pCultureName, (int) state)
        {
        }

        public MedianamikRedirect(Guid pNodeId, string pCultureName)
            : this(pNodeId, pCultureName, default(int?))
        {
        }

        public MedianamikRedirect(Guid pNodeId)
            : this(pNodeId, CultureInfo.CurrentUICulture.Name, default(int?))
        {
        }

        private MedianamikRedirect(Guid pNodeId, string pCultureName, int? pContentState)
            : this()
        {
            if (pContentState.HasValue)
            {
                CurrentGeneration = pContentState.Value;
            }
            else
            {
                CurrentGeneration = HttpContext.Current == null
                                        ? (int) ContentState.Published
                                        : new Utility().GetGeneration(HttpContext.Current.Request);
            }

            var q = Query();
            q.WHERE("NodeId", pNodeId.ToString());
            q.WHERE("CultureName", pCultureName);
            q.WHERE("Generation", CurrentGeneration);
            LoadAndCloseReader(q.ExecuteReader());
        }

        [XmlAttribute("Id")]
        [Bindable(true)]
        public Guid Id
        {
            get { return GetColumnValue<Guid>("Id"); }
            set { base.SetColumnValue("Id", value); }
        }

        [XmlAttribute("NodeId")]
        [Bindable(true)]
        public Guid NodeId
        {
            get { return GetColumnValue<Guid>("NodeId"); }
            set { base.SetColumnValue("NodeId", value); }
        }

        [XmlAttribute("Revision")]
        [Bindable(true)]
        public int? Revision
        {
            get { return GetColumnValue<int?>("Revision"); }
            set { base.SetColumnValue("Revision", value); }
        }

        [XmlAttribute("CultureName")]
        [Bindable(true)]
        public string CultureName
        {
            get { return GetColumnValue<string>("CultureName"); }
            set { base.SetColumnValue("CultureName", value); }
        }

        [XmlAttribute("IsActive")]
        [Bindable(true)]
        public bool IsActive
        {
            get { return GetColumnValue<bool>("IsActive"); }
            set { base.SetColumnValue("IsActive", value); }
        }

        [XmlAttribute("IsSubEntity")]
        [Bindable(true)]
        public bool IsSubEntity
        {
            get { return GetColumnValue<bool>("IsSubEntity"); }
            set { base.SetColumnValue("IsSubEntity", value); }
        }

        [XmlAttribute("Generation")]
        [Bindable(true)]
        public int Generation
        {
            get { return GetColumnValue<int>("Generation"); }
            set { base.SetColumnValue("Generation", value); }
        }

        [XmlAttribute("HasModifications")]
        [Bindable(true)]
        public bool HasModifications
        {
            get { return GetColumnValue<bool>("HasModifications"); }
            set { base.SetColumnValue("HasModifications", value); }
        }

        [XmlAttribute("CreatedOn")]
        [Bindable(true)]
        public DateTime CreatedOn
        {
            get { return GetColumnValue<DateTime>("CreatedOn"); }
            set { base.SetColumnValue("CreatedOn", value); }
        }

        [XmlAttribute("Depth")]
        [Bindable(true)]
        public int Depth
        {
            get { return GetColumnValue<int>("Depth"); }
            set { base.SetColumnValue("Depth", value); }
        }

        [XmlAttribute("ParentNodeId")]
        [Bindable(true)]
        public Guid? ParentNodeId
        {
            get { return GetColumnValue<Guid?>("ParentNodeId"); }
            set { base.SetColumnValue("ParentNodeId", value); }
        }

        [XmlAttribute("Path")]
        [Bindable(true)]
        public string Path
        {
            get { return GetColumnValue<string>("Path"); }
            set { base.SetColumnValue("Path", value); }
        }

        [XmlAttribute("Position")]
        [Bindable(true)]
        public short Position
        {
            get { return GetColumnValue<short>("Position"); }
            set { base.SetColumnValue("Position", value); }
        }

        [XmlAttribute("TypeId")]
        [Bindable(true)]
        public Guid TypeId
        {
            get { return GetColumnValue<Guid>("TypeId"); }
            set { base.SetColumnValue("TypeId", value); }
        }

        [XmlAttribute("ModifiedOn")]
        [Bindable(true)]
        public DateTime? ModifiedOn
        {
            get { return GetColumnValue<DateTime?>("ModifiedOn"); }
            set { base.SetColumnValue("ModifiedOn", value); }
        }

        [XmlAttribute("CreatedBy")]
        [Bindable(true)]
        public string CreatedBy
        {
            get { return GetColumnValue<string>("CreatedBy"); }
            set { base.SetColumnValue("CreatedBy", value); }
        }

        [XmlAttribute("QueryString")]
        [Bindable(true)]
        public string QueryString
        {
            get { return GetColumnValue<string>("QueryString"); }
            set { base.SetColumnValue("QueryString", value); }
        }

        [XmlAttribute("RedirectFrom")]
        [Bindable(true)]
        public string RedirectFrom
        {
            get { return GetColumnValue<string>("RedirectFrom"); }
            set { base.SetColumnValue("RedirectFrom", value); }
        }

        [XmlAttribute("RedirectTo")]
        [Bindable(true)]
        public Guid? RedirectTo
        {
            get { return GetColumnValue<Guid?>("RedirectTo"); }
            set { base.SetColumnValue("RedirectTo", value); }
        }

        [XmlAttribute("RedirectCode")]
        [Bindable(true)]
        public int? RedirectCode
        {
            get { return GetColumnValue<int?>("RedirectCode"); }
            set { base.SetColumnValue("RedirectCode", value); }
        }

        public CultureInfo Culture
        {
            get { return CultureInfo.GetCultureInfo(CultureName); }
        }

        public static MedianamikRedirect FetchByNodeID(Guid? pNodeId, string pCultureName, ContentState pState)
        {
            if (pNodeId.HasValue)
            {
                return new MedianamikRedirect(pNodeId.Value, pCultureName, pState);
            }
            else return new MedianamikRedirect();
        }

        public static CacheDependency GetCacheDependency()
        {
            var deps = new[]
                           {
                               new SqlCacheDependency("Medianamik.Client", "gen_MedianamikRedirect"),
                           };


            var aggDep = new AggregateCacheDependency();


            aggDep.Add(deps);

            return aggDep;
        }

        public BaseWebType BaseWebTypeRecord
        {
            get { return BaseWebType.FetchByNodeID(RedirectTo, CultureName, (ContentState) CurrentGeneration); }
        }

        public struct Columns
        {
            public static string Id = @"Id";

            public static string NodeId = @"NodeId";

            public static string Revision = @"Revision";

            public static string CultureName = @"CultureName";

            public static string IsActive = @"IsActive";

            public static string IsSubEntity = @"IsSubEntity";

            public static string Generation = @"Generation";

            public static string HasModifications = @"HasModifications";

            public static string CreatedOn = @"CreatedOn";

            public static string Depth = @"Depth";

            public static string ParentNodeId = @"ParentNodeId";

            public static string Path = @"Path";

            public static string Position = @"Position";

            public static string TypeId = @"TypeId";

            public static string ModifiedOn = @"ModifiedOn";

            public static string CreatedBy = @"CreatedBy";

            public static string QueryString = @"QueryString";

            public static string RedirectFrom = @"RedirectFrom";

            public static string RedirectTo = @"RedirectTo";

            public static string RedirectCode = @"RedirectCode";
        }

        public new CT GetColumnValue<CT>(string columnName)
        {
            return base.GetColumnValue<CT>(columnName);
        }

        public object GetColumnValue(string columnName)
        {
            return base.GetColumnValue<object>(columnName);
        }
    }

    #region Medianamik

    public interface IMedianamikRedirect : IMedianamikDTO
    {
        string QueryString { get; }

        string RedirectFrom { get; }

        Guid? RedirectTo { get; }

        int? RedirectCode { get; }
    }

    #region BasePage

    #endregion

    #endregion
}